import { BaseCrudApi } from "~/api/util/BaseService";
import { GenDtoField, GenField, GenTable, GenVoField } from "~/api/system/gen/types";
import { reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

export class GenFieldApi extends BaseCrudApi<GenField> {

  baseUrl = "/gen/field";

}

export class GenDtoFieldApi extends BaseCrudApi<GenDtoField> {

  baseUrl = "/gen/dto-field";

}

export class GenVoFieldApi extends BaseCrudApi<GenVoField> {

  baseUrl = "/gen/vo-field";

}

export class GenTableApi extends BaseCrudApi<GenTable> {

  baseUrl = "/gen/table";

  previewState = reactive({
    visible: false,
    title: "预览",
    data: {},
  });

  onPreview(id: string) {
    this.get("/preview/" + id).then(res => {
      if (res.ok && res.data) {
        this.previewState.data = res.data;
        this.previewState.visible = true;
      }
    });
  }

  onClosePreview() {
    this.previewState.data = {};
    this.previewState.visible = false;
  }

  async onZip(id: string) {
    await this.downLoadZip("/getZip?id=" + id);
  }

  async onVueZip(id: string) {
    await this.downLoadZip("/getVueZip?id=" + id);
  }

  onGenCode(id: string) {
    const _this = this;
    ElMessageBox.confirm("生成代码会将上次代码覆盖", "确认生成", {
      confirmButtonText: "生成代码",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      this.get("/genCode/" + id).then(res => {
        if (res.ok && res.data) {
          ElMessage.success("完成");
        }
      });
    }).catch((e) => {
      console.log("Oops errors! " + e);
    });
  }

  onGenVueCode(id: string) {
    const _this = this;
    ElMessageBox.confirm("生成代码会将上次代码覆盖", "确认生成", {
      confirmButtonText: "生成代码",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      this.get("/genCode/" + id).then(res => {
        if (res.ok && res.data) {
          ElMessage.success("完成");
        }
      });
    }).catch((e) => {
      console.log("Oops errors! " + e);
    });
  }

}
